<?php

namespace app\commission;

use app\models\order\OrderItem;
use app\models\product\Product;
use app\models\product\ProductAreaList;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserLevel;
use app\services\Chain;

/**
 *
 * new upgrade
 *
 * Class UpGrade
 * @package app\commission
 */
class UpNew
{
    protected $order;
    protected $set;

    public function __construct(\app\models\order\Order $order)
    {
        $this->order = $order;

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        // 只有报单区才可以升级
        if (ProductAreaList::checkInArea($product_id, Product::$areaMap['REPORT'])) {

            // 后台设置的参数
            $this->set = SetUserUp::getLastOne();

            // 499 升级代理
            // $total_amount = 499;
            // 改成599了
            $total_amount = $this->set->vip1;
            $total_price = $this->order->total_pay - $this->order->total_freight;

            $total_amount = floatval($total_amount);
            $total_price = floatval($total_price);

            // var_dump($total_amount==$total_price);exit;

            // if ($total_price == $total_amount) {
            if ($total_price >= $total_amount) {
                $user = User::findOne(['id' => $order->user_id]);
                if ($user && $user->level < UserLevel::$keys['vip1']) {
                    $user->level = UserLevel::$keys['vip1'];
                    $user->save();
                }
            }

            // 升级关系链
            $this->chainUp($order->user_id);

        }
    }

    // 关系链 升级
    protected function chainUp($user_id)
    {
        // 包含本人
        $chain = Chain::getReferrerChain($user_id, [], true);

        foreach ($chain as $u) {

            // 必须要是 代理及以上等级
            if ($u->level < UserLevel::$keys['vip1']) {
                continue;
            }

            // 直推2个代理, 成为合伙人
            if ($u->level < UserLevel::$keys['vip2']) {

                // $count = 2;
                $count = $this->set->vip2;
                $recommend = Chain::getChildren($u->id);
                $sum = 0;

                foreach ($recommend as $v) {
                    // 需要推的是代理及以上
                    if ($v['level'] >= UserLevel::$keys['vip1']) {
                        $sum += 1;
                    }
                }

                if ($sum >= $count) {
                    $u->level = UserLevel::$keys['vip2'];
                    $u->save();
                }
            }


            // vip3
            if ($u->level < UserLevel::$keys['vip3']) {
                $count = $this->set->vip3;
                // $children = Chain::getChildrenAll($u->id, [
                //     UserLevel::$keys['vip1'],
                //     UserLevel::$keys['vip2'],
                //     UserLevel::$keys['vip3'],
                //
                //     // 地区是另一种等级
                //     // UserLevel::$keys['svip1'],
                //     // UserLevel::$keys['svip2'],
                //     // UserLevel::$keys['svip3'],
                // ]);

                // 这里改成了(直推)20个, 就直接升级
                $children = Chain::getChildrenByLevel($u->id, [
                    UserLevel::$keys['vip1'],
                    UserLevel::$keys['vip2'],
                    UserLevel::$keys['vip3'],
                ]);

                // var_dump($children);exit;

                if (count($children) + 1 >= $count) {
                    $u->level = UserLevel::$keys['vip3'];
                    $u->save();
                }
            }

        }
    }
}